﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WITM.BLL;
using WITM.DAL;

namespace WITMTestApplication
{
    public partial class ItemsTestForm : Form
    {
        private UserBL userBl;
        private SpendingCategoryBL spendingCategoryBl;
        private SpendingItemBL spendingItemBl;

        public ItemsTestForm()
        {
            InitializeComponent();
            InitializeDB(false);
            InitializeItemsGrid();
            LoadUsersComboBox();
        }

        private void InitializeItemsGrid()
        {
            dgvItems.Columns.Add("colItemID", "ID");
            dgvItems.Columns.Add("colDescription", "Description");
        }

        private void LoadUsersComboBox()
        {
            IEnumerable<Users> users = userBl.GetAll();
            var usersFormated = from u in users
                                select new
                                {
                                    ID = u.UserID,
                                    Name = String.Format("{0} {1}", u.FirstName, u.LastName)
                                };
            cbUsers.ValueMember = "ID";
            cbUsers.DisplayMember = "Name";
            cbUsers.DataSource = usersFormated.ToList();
        }

        private void LoadCategoriesComboBox()
        {
            int userID = (int)cbUsers.SelectedValue;
            IEnumerable<SpendingCategories> categories = spendingCategoryBl.GetAllPerUser(userID);
            cbCategories.ValueMember = "CategoryID";
            cbCategories.DisplayMember = "Description";
            cbCategories.DataSource = categories;
        }

        private void LoadSpendingItems()
        {
            long categoryID = (long)cbCategories.SelectedValue;
            IEnumerable<SpendingItems> items = spendingItemBl.GetAllPerCategory(categoryID);
            dgvItems.Rows.Clear();
            foreach (SpendingItems item in items)
            {
                dgvItems.Rows.Add(item.ItemID, item.Description);
            }
        }

        private void InitializeDB(bool tracking)
        {
            WhereIsTheMoneyDBEntities context = new WhereIsTheMoneyDBEntities();
            if (!tracking)
            {
                context.Users.MergeOption = System.Data.Objects.MergeOption.NoTracking;
                context.SpendingCategories.MergeOption = System.Data.Objects.MergeOption.NoTracking;
                context.SpendingItems.MergeOption = System.Data.Objects.MergeOption.NoTracking;
            }
            UnitOfWork unitOfWork = new UnitOfWork(context);
            userBl = new UserBL(unitOfWork);
            spendingCategoryBl = new SpendingCategoryBL(unitOfWork);
            spendingItemBl = new SpendingItemBL(unitOfWork);
        }

        private void cbUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCategoriesComboBox();
        }

        private void cbCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadSpendingItems();
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            InitializeDB(true);
            long categoryID = (long)cbCategories.SelectedValue;
            SpendingCategories category = spendingCategoryBl.GetCategoryByID(categoryID);
            SpendingItems item = new SpendingItems()
            {
                Description = txtItemDesc.Text,
                SpendingCategories = category
            };
            spendingItemBl.Add(item);
            txtItemDesc.Text = String.Empty;
            LoadSpendingItems();
        }
    }
}
